package commands;

import IA.CustomerQueue;
import IA.InformationAggregator;
import IA.Table;
import IA.WaitingParty;
import java.util.Iterator;

//finds the first party (waiting longest) that can fit into the available table
//assumes there is only one available table
//this will be called as soon as one table is free
public class CmdCallNextWaitParty extends Command {

    public CmdCallNextWaitParty(InformationAggregator ia) {
        super(ia);
    }

    @Override
    public Object execute() {
        CustomerQueue cq = super.ia.getCustomerQueue();
        //loop through available tables
        //i wrote this so we can choose based on table sizes
        for (Iterator<Table> TableIterator = super.ia.getTableCollection().getAvailTableIterator(); TableIterator.hasNext();) {
            Table t = TableIterator.next();
            //loop through waiting parties, longest first
            for (Iterator<WaitingParty> PartyIterator = super.ia.getCustomerQueue().getLongestFirstIterator(); PartyIterator.hasNext();) {
                WaitingParty p = PartyIterator.next();
                //if party fits into table, call them and create a service for them
                if (t.getNumSeats() >= p.getNumCustomers()) {
                    //removes from queue and removes party from systemclock notify list
                    super.ia.getCustomerQueue().CallParty(p);
                    new CmdStartNewService(super.ia, p.getNumCustomers(), t.getID(), p.getEatTime()).execute();
                    new CmdSimulateOrder(ia, ia.getServiceCollection().getLastAddedService()).execute();
                    return ia.getServiceCollection().getLastAddedService();
                }
            }
        }
        //if no table fit the waiting customers, return nothing as well (although nothing will have been done)
        return null;
    }
}
